package com.tensquare.friend.service;

import com.tensquare.friend.dao.FriendDao;
import com.tensquare.friend.dao.NoFriendDao;
import com.tensquare.friend.model.Friend;
import com.tensquare.friend.model.NoFriend;
import com.tensquare.util.NotNullHelps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;


@Slf4j
@Service
@Transactional
public class FriendService {


    @Autowired
    private FriendDao friendDao ;

    @Autowired
    private NoFriendDao nofriendDao ;

    private Integer sqlResult = 1;

    public Integer addFriend(String userId , String friendId){
        Friend friend = friendDao.findFirstByUseridAndFriendid(userId, friendId);
        if (NotNullHelps.isNotNull(friend) == true) {
            return  0 ;
        }
        friend = new Friend(userId, friendId, "0");
        friendDao.save(friend);
        friend = friendDao.findFirstByUseridAndFriendid(friendId, userId);
        if (NotNullHelps.isNotNull(friend)) {
            friendDao.updateIsLike("1", userId, friendId);
            friendDao.updateIsLike("1", friendId, userId);
        }
        return sqlResult ;
    }

    public Integer addNoFriend(String userId , String friendId){
        NoFriend noFriend = nofriendDao.findFirstByUseridAndFriendid(userId, friendId);
        if (NotNullHelps.isNotNull(noFriend) == true) {
            return  0 ;
        }
        noFriend = new NoFriend(userId, friendId);
        nofriendDao.save(noFriend);
        return sqlResult ;
    }

    public void deleteFriend(String userId , String friendId){
        friendDao.deleteFriend(userId, friendId);
        friendDao.updateIsLike("0",friendId, userId);
        NoFriend noFriend = new NoFriend(userId, friendId);
        nofriendDao.save(noFriend);
    }

}
